<template>
  <el-dialog :close-on-click-modal="false" :title="title" :visible.sync="dialogFormVisible" width="30%" @close="close"
    :append-to-body="true">
    <el-form ref="form" :model="form" :rules="rules" label-width="0.8rem">
      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item label="单位名称" prop="danweimc">
            <el-input v-model.trim="form.danweimc" autocomplete="off"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="单位代码" prop="danweidm">
            <el-input v-model.trim="form.danweidm" autocomplete="off"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="所属仓库" prop="cangkuid">
            <el-cascader :options="cangkutree" v-model="form.cangkuid"
              :props="{ checkStrictly: true, label: 'text', value: 'id', children: 'children', emitPath: false }"
              :show-all-levels="false" clearable></el-cascader>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { insertZhuguandw, updateZhuguandw } from '@/api/biaozhun.js'
import { getTree } from '@/api/institution.js'

export default {
  name: 'UserManagementEdit',
  data() {
    return {
      form: {},
      rules: {
        danweimc: [
          { required: true, trigger: 'blur', message: '请输入用户名' },
        ],
        danweidm: [{ required: true, trigger: 'blur', message: '请输入密码' }],
        cangkuid: [
          { required: true, trigger: 'blur', message: '请选择库房' },
        ],
      },
      title: '',
      dialogFormVisible: false,
      cangkutree: []
    }
  },
  created() { },
  async mounted() {
    //仓库
    const { data } = await getTree();
    this.cangkutree = data
  },
  methods: {
    showEdit(row) {
      if (!row) {
        this.title = '新增'
      } else {
        this.title = '编辑'
        this.form = Object.assign({}, row)
      }
      this.dialogFormVisible = true
    },
    close() {
      this.$refs['form'].resetFields()
      this.form = this.$options.data().form
      this.dialogFormVisible = false
    },
    async save() {
      this.$refs['form'].validate(async (valid) => {
        if (valid) {
          var data = {}
          if (this.title === '新增') {
            data = await insertZhuguandw(this.form)
          } else {
            data = await updateZhuguandw(this.form)
          }
          this.$baseMessage(data.msg, 'success')
          this.$emit('fetch-data')
          this.close()
        } else {
          return false
        }
      })
    },
  },
}
</script>
<style lang="scss" scoped>
::v-deep {
  .el-select {
    width: 100%;
  }

  .el-form-item__label {
    line-height: 16px !important;
  }

  .el-date-editor.el-input {
    width: 100%;
  }

  .el-cascader--small {
    width: 100%;
  }
}
</style>